
project , uses(../data_output/legislators_klarner.dta)


	***********
	** MICHIGAN
	** Life-time term limites: 3 x 2-year terms.
	** First year: 1998	
	***********	
	** term limits house: 3 two-year terms. Life-time ban. Counting beginning in 1992. Example: Dianne Byrum was elected in 1990, 1992, 2002 and 2004. In 2006, she was term limited.
	** MI prepare Klarner
	use ../data_output/legislators_klarner.dta, clear
	keep if state=="MI" & chamber=="house"	
	keep if regexm(term, "1993|1995|1997|1999|2001|2003|2005|2007|2009|2011|2013|2015")
	gen lastname = proper(regexs(1)) if regexm(CandName,"^(.*),")
	gen firstname = proper(regexs(1)) if regexm(CandName,", (['-a-z]+)")
	gen mergename = firstname + " " + lastname
	replace mergename="Lamar Lemmons Iii" if CandName=="LEMMONS, LAMAR III"
	replace mergename="Lamar Lemmons Jr." if CandName=="LEMMONS, LAMAR JR."	
	br if regexm(lower(CandName), "kenn")
	save ../data_output/legislators_MI_house_1993_2016.dta, replace

	gen e_won = elections_won
	qui forval i= 1960(2)1990{ //years enacted
		replace e_won = subinstr(e_won, "`i';", "",.)
	}
	tostring year, gen(regexp)
	replace regexp = "^([^!]+"+regexp+";)"
	gen previous_wins = regexs(1) if regexm(e_won, regexp)
	egen prev_terms = noccur(previous_wins) , string(";")
	gen termlimited=prev_terms==3	
	save ../data_output/legislators_MI_house_1993_2016.dta, replace
project , creates(../data_output/legislators_MI_house_1993_2016.dta)
	
project , original(../data_input/MI/mi_bills.csv)	
	*Michigan merge with bill data
	import delimited using ../data_input/MI/mi_bills.csv,  clear  delimiters(";") varnames(1)  encoding("utf-8")  bindquotes(strict)
	
	gen Name = name
	rename description subject
	replace name = trim(usubinstr(proper(name),"Iii", "",.))
	replace name = trim(usubinstr(proper(name),"Jr.", "",.))
	gen firstname = proper(regexs(1)) if regexm(name,"^(['-a-z]+) ")
	gen lastname = proper(regexs(1)) if regexm(name,"(['-a-z]+)$")
	replace lastname = regexr(lastname, "[A-Z][.]","")
	gen mergename = firstname + " " + lastname
	
	replace mergename="Robert Emerson" if mergename=="Robert L. Emerson" | mergename=="Bob Emerson"
	replace mergename="Bill Bobier" if mergename=="William Bobier" 
	replace mergename="Bob Brackenridge" if mergename=="Robert Brackenridge" & regexm(term, "1995|1997")
	replace mergename="Bobby Brown" if mergename=="Bob Brown" 
	replace mergename="Barb Vanderveen" if mergename=="Barb Veen" 
	replace mergename="Peter Lund" if mergename=="Pete Lund" 
	replace mergename="David Woodward" if mergename=="Dave Woodward" 
	replace mergename="Edward Vaughn" if mergename=="Ed Vaughn" 
	replace mergename="Elizabeth Brater" if mergename=="Liz Brater" 
	replace mergename="Frederick Miller" if mergename=="Fred Miller" 
	replace mergename="Hoonyung Hopgood" if mergename=="Hoon-Yung Hopgood" 
	replace mergename="Joseph Haveman" if mergename=="Joe Haveman" 
	replace mergename="Irma Clarkcoleman" if mergename=="Irma Clark" 
	replace mergename="Fred Durhal" if mergename=="Fred Durhal," 
	replace mergename="Gerald Vanwoerkom" if name=="Gerald Van Woerkom" 
	replace mergename="Triette Lipseyreeves" if mergename=="Triette Reeves" 
	replace mergename="Sandy Caul" if mergename=="Sandra Caul" 
	replace mergename="Marcia Hoveywright" if mergename=="Marcia Hovey-Wright" 
	replace mergename="Kenneth Bradstreet" if mergename=="Ken Bradstreet" 
	replace mergename="Jerry Vanderroes" if mergename=="Jerry Roest" 
	replace mergename="M. Pumford" if mergename=="Mike Pumford" 
	replace mergename="William Oneil" if mergename=="William 27Neil" 
	replace mergename="Margaret Obrien" if mergename=="Margaret 27Brien" 
	replace mergename="Artina Tinsleyhardman" if mergename=="Artina Hardman" 
	replace mergename="Alberta Tinsleytalabi" if mergename=="Alberta Tinsley-Talabi" 
	replace mergename="Patrick Gagliardi" if mergename=="Pat Gagliardi" 
	replace mergename="Philip Potvin" if mergename=="Phil Potvin" 
	replace mergename="Tom Middleton" if mergename=="Thomas Middleton" 
	replace mergename="Carl Gnodike" if mergename=="Carl Gnodtke" 
	replace mergename="Joseph Young" if mergename=="Joseph Young," 
	replace mergename="Jud Gilbert" if mergename=="Jud Ii" 
	replace mergename="Jud Gilbert" if mergename=="Judson Gilbert" 
	replace mergename="Lamar Lemmons Jr." if Name=="LaMar Lemmons, Jr."  | Name=="LaMar Lemmons"
	replace mergename="Lamar Lemmons Iii" if Name=="LaMar Lemmons III" 
	replace mergename="James Bolger" if mergename=="Jase Bolger" 
	replace mergename="Morris Hood" if mergename=="Morris Hood," 
	replace mergename="Phillip Pavlov" if mergename=="Phil Pavlov" 
	replace mergename="Sam Singh" if mergename=="Samir Singh" 
	replace mergename="Thomas Hooker" if mergename=="Tom Hooker" 
	replace mergename="Bill Mcconico" if mergename=="William Mcconico" 
	replace mergename="Bradford Jacobsen" if mergename=="Brad Jacobsen" 
	replace mergename="Dave Jaye" if mergename=="David Jaye" 
	replace mergename="David Agema" if mergename=="Dave Agema" 
	replace mergename="Daniel Scripps" if mergename=="Dan Scripps" 
	replace mergename="Dominic Jacobetti" if mergename=="D.J. Jacobetti" 
	replace mergename="Edward Canfield" if mergename=="Ed Canfield" 
	replace mergename="Charlie Harrison" if mergename=="Charles Harrison," 
	replace mergename="Henry Vaupel" if mergename=="Hank Vaupel" 
	replace mergename="James Marleau" if mergename=="Jim Marleau" 
	replace mergename="Rob Verheulen" if mergename=="Robert Verheulen" 
	replace mergename="Sherry Gaydagnogo" if mergename=="Sherry Gay-Dagnogo" 
	replace mergename="Stacy Erwinoakes" if mergename=="Stacy Oakes" 
	replace mergename="Steve Shepich" if mergename=="Stephen Shepich" 
	replace mergename="William Bryant" if mergename=="William Bryant," 
	replace mergename="William Vanregenmorter" if mergename=="William Regenmorter" 
	replace mergename="Willis Bullard" if mergename=="Willis Bullard," 
	replace mergename="James Oneill" if mergename=="James 27Neill," 
	replace mergename="George Cushingberry" if mergename=="George Cushingberry," 
	replace mergename="Stacy Oakes" if mergename=="Stacy Erwinoakes" & regexm(term, "2013")
	replace mergename="Alberta Tinsleytalabi" if mergename=="Alberta Talabi" & regexm(term, "2011")
	rename name sponsor

	merge m:1 mergename term using ../data_output/legislators_MI_house_1993_2016.dta
	drop if _merge==1
	drop _merge
		//7 legislators are missing in the bills data. Janet Kukuk (died on November 19, 2000, and never assumed office); Gregory Pitoniak was appointed to Mayor the city of Taylor and resigned;
	// Tom Alley ran for the state senate in 1998, but appear to have remained in office until then; Joe Young appear to have retired in 1993; Philip Hoffman retired in 1993
	//Bob Brackenridge; Vincent Porreca
	
	drop sponsor
	gen sponsor = 1 if bill!=""
	gen law = regexm(bill, "[(]PA")		
	save ../data_output/bills_by_sponsor_MI_house_1993_2016.dta, replace
project, creates(../data_output/bills_by_sponsor_MI_house_1993_2016.dta)

	use ../data_output/bills_by_sponsor_MI_house_1993_2016.dta, clear
	collapse (sum) sponsor law (first) elections_* terms_in_office win TermLengthActual CandName district chamber state party mergename inoffice year termlimited vtsh, by(CandId term )
	replace law = . if term=="1993-1994"
	save ../data_output/total_bills_by_sponsor_MI_house_1993_2016.dta, replace
project, creates(../data_output/total_bills_by_sponsor_MI_house_1993_2016.dta) 

	
	**Voting
	
	use ../data_output/legislators_klarner.dta, clear
	gen lastname = regexs(1) if regexm(lower(CandName),"([^,]+), [^!]")
	gen firstname = regexs(1) if regexm(lower(CandName),"[^,]+, ([^!]+)")
	keep if state=="MI" & chamber=="house"
	keep if regexm(term, "2001|2003|2005|2007|2009|2011|2013|2015")
	gen os_mergename = lastname
	bys  os_mergename term: gen n = _n
	by  os_mergename term: egen mean_n=mean(n)
	replace os_mergename = lastname + ", " + firstname if mean_n>1  //firstname  + " " +  lastname //firstname+ ". "
	drop n mean_n
	tempfile tmp
	save `tmp', replace
	
	
project, original(../data_input/MI/mi_votes.csv)	
	import delimited using ../data_input/MI/mi_votes.csv,  clear  delimiters(";") bindquotes(strict) encoding("utf-8") varnames(1)
	keep if regexm(location, "floor")
	keep if chamber=="house"
	rename session term
	gen os_mergename = lower(name)
	replace os_mergename = lower(os_mergename)
	replace os_mergename = subinstr(os_mergename,"’","",.)
	replace os_mergename = subinstr(os_mergename,"'","",.)
	replace os_mergename = subinstr(os_mergename,"-","",.)
	replace os_mergename = subinstr(os_mergename,"van ","van",.)	
	replace os_mergename = subinstr(os_mergename,"vander ","vander",.)	
	replace os_mergename = "brown, bobby"	if os_mergename=="brown, b." & regexm(term, "2001")
	replace os_mergename = "brown, cameron"	if os_mergename=="brown, c." & regexm(term, "2001")
	replace os_mergename = "brown, rich"	if os_mergename=="brown, r." & regexm(term, "2001")
	replace os_mergename = "clarkcoleman"	if os_mergename=="clark" & regexm(term, "2001")
	replace os_mergename = "vanderroes"	if os_mergename=="vanderroest" & regexm(term, "2001")
	replace os_mergename = "tinsleyhardman"	if os_mergename=="hardman" & regexm(term, "2001|2003")
	replace os_mergename = "lipseyreeves"	if os_mergename=="reeves" & regexm(term, "2001|2003")
	replace os_mergename = "jones"	if os_mergename=="jones, rick" & regexm(term, "2005")
	replace os_mergename = "lemmons, lamar iii"	if os_mergename=="lemmons, iii" & regexm(term, "2005")
	replace os_mergename = "lemmons, lamar jr."	if os_mergename=="lemmons, jr." & regexm(term, "2005")
	replace os_mergename = "jones, robert b."	if os_mergename=="jones, robert" & regexm(term, "2007|2009")
	replace os_mergename = "brown, lisa"	if os_mergename=="brown, l." & regexm(term, "2009")
	replace os_mergename = "brown, terry l."	if os_mergename=="brown, t." & regexm(term, "2009")
	replace os_mergename = "schmidt, roy"	if os_mergename=="schmidt, r." & regexm(term, "2009|2011")
	replace os_mergename = "schmidt, wayne a."	if os_mergename=="schmidt, w." & regexm(term, "2009|2011")
	replace os_mergename = "scott, bettie cook"	if os_mergename=="scott, b." & regexm(term, "2009")
	replace os_mergename = "scott, paul"	if os_mergename=="scott, p." & regexm(term, "2009")
	replace os_mergename = "erwinoakes"	if os_mergename=="oakes" & regexm(term, "2011")
	replace os_mergename = "tinsleytalabi"	if os_mergename=="talabi" & regexm(term, "2011|2015")
	replace os_mergename = "roberts"	if os_mergename=="roberts,s" & regexm(term, "2013")	
	replace os_mergename = "miller, aaron"	if os_mergename=="miller,a" & regexm(term, "2015")
	replace os_mergename = "miller, derek"	if os_mergename=="miller,d" & regexm(term, "2015")
	replace os_mergename = "roberts, brett"	if os_mergename=="roberts,b" & regexm(term, "2015")
	replace os_mergename = "roberts, sarah"	if os_mergename=="roberts,s" & regexm(term, "2015")

	merge m:1  os_mergename term using `tmp'

	// 1 obs not merging
	drop if _merge==1
	drop _merge	
	
	replace vote = "1" if vote=="y"
	replace vote = "-1" if vote=="n"
	replace vote = "0" if regexm(vote,"1")==0
	gen floorvotes = regexm(vote, "1")
	destring vote, replace
	egen vote_id = group(term chamber bill date location ) , missing
	bys party vote_id : egen party_mean=mean(vote)
	gen party_vote = 0
	replace party_vote = 1 if party_mean>0
	replace party_vote = -1 if party_mean<0
	gen vote_party_line = vote==party_vote
	gen vote_against_party = vote==-party_vote
	bys vote_id: egen votemargin = mean(vote)
	gen close_floorvotes = floorvotes if abs(votemargin)<.1			
	
	save ../data_output/floorvotes_MI_2001_2016.dta, replace
project , creates(../data_output/floorvotes_MI_2001_2016.dta)
	
	use ../data_output/floorvotes_MI_2001_2016.dta, clear
	collapse  (mean) vote_party_line vote_against_party (sum)  *floorvotes (first) elections_* terms_in_office win TermLengthActual CandName district chamber state party  inoffice year  vtsh, by(CandId term )
	
	save ../data_output/total_floorvotes_MI_2001_2016.dta, replace
project , creates(../data_output/total_floorvotes_MI_2001_2016.dta)
	
	
